const enterpriseVue = new Vue({
    el: '#app',
    data() {
        return {
            rules: {

            },
            cagie: '',
            form: {//储值卡方案表
                Giftt_Card_Scheme_SerialNumber: "",//储值卡方案流水号
                Gift_Card_Scheme_Name: '',//储值卡方案名称
                Gift_Card_Quantity: '',
                Effective_Date: '',//有效期(长期有效为1可选)
                Gift_Card_Page_SerialNumber: '',//储值卡版面
                Start_Date: '',//上架时间
                End_Date: '',//结束时间
                Gift_Card_Article_Quantity: '',//付款方式(1微信支付,2支付宝支付,3电汇)
                Buyer_Enterprise_Info_SerialNumber: '',//被制卡企业
                Enterprise_Info_SerialNumber: $.cookie("enterprise"),//本企
                Children: {
                    gift_card_scheme_storescope: [{//适用店铺表
                        Gift_Card_Scheme_StoreScope_SerialNumber: "",//主键
                        Gift_Card_Scheme_SerialNumber: "",//外键
                        Store_SerialNumber: "",//门店流水号表
                    }],
                    gift_card_scheme_article: [{//适用商品表
                        Gift_Card_Scheme_Article_SerialNumber: "",//主键
                        Gift_Card_Scheme_SerialNumber: "",//外键
                        Article_SerialNumber: "",//商品主键
                        Quantity: "",
                    }],
                }
            },
            Articles: [],
            Goods: [],
            options: [{
                value: '选项1',
                label: '黄金糕'
            }, {
                value: '选项2',
                label: '双皮奶'
            }, {
                value: '选项3',
                label: '蚵仔煎'
            }, {
                value: '选项4',
                label: '龙须面'
            }, {
                value: '选项5',
                label: '北京烤鸭'
            }],
            radio: '2',
            II_total: 1,
            activeName: 'first',
            tableData: [],
            tableData1: [],
            loading: false,
            Employees: [],
            PayWay: [{
                value: '1',
                label: '微信支付'
            }, {
                value: '2',
                label: '支付宝支付'
            }, {
                value: '3',
                label: '电子汇账'
            },],
            Categorys: [{
                label: '按商品类别',
                value: '0',
            }, {
                label: '按商品品牌',
                value: '1',
            }],
            Enterprises: [],
            ArticleByCategorys: [],
            Stores: [],
            IO_form: {
                page: 1,
                limit: 10,
                Category_SerialNumber: JSON.stringify([]),
                Brand_SerialNumber: JSON.stringify([]),
                // Goods_Brand_SerialNumber:JSON.stringify([]),
                Article_Name: ""
            },
            IO_form1: {
                page: 1,
                limit: 10,
                Department_SerialNumber: JSON.stringify([]),
            },
            total1: 0,
            total2: 0,
            isPermanent: true,
            organization: [],
            props: {
                expandTrigger: 'hover',
                label: 'text',
                value: 'value',
                children: 'children',
                multiple: true
            },
            Cardpages: [],
            CardpagesImg: '',
            srcList: []
        }
    },
    mounted() {
        this.getBuyer_Enterprise()
        this.getStores()
        this.getCardpage()

    },
    methods: {
        handleSizeChange1(val) {
            this.IO_form.page = 1;
            this.IO_form.limit = val;
        },
        //号码按键查询
        handleCurrentChange1(val) {
            this.IO_form.page = val;
        },
        handleSizeChange2(val) {
            this.IO_form1.page = 1;
            this.IO_form1.limit = val;
        },
        //号码按键查询
        handleCurrentChange2(val) {
            this.IO_form1.page = val;
        },
        getBuyer_Enterprise() {
            const this_ = this
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Data: 'enterprise_info',
                    isNullEnterprise: true
                },
                success: function (res) {
                    this_.Enterprises = res
                    this_.Enterprises = this_.Enterprises.filter(item => item.Enterprise_Name != '系统管理')
                    console.log(this_.Enterprises)
                }
            })
        },
        getEmployees(Enterprise_Info_SerialNumber) {
            const this_ = this
            $.ajax({
                url: DomainName + '/getOrganizationAction?token=' + $.cookie('token'),
                type: 'POST',
                data: {
                    Enterprise_Info_SerialNumber,
                },
                success: function (res) {
                    console.log(res)
                    this_.Employees = res.Data
                }
            })
        },
        EnterChange(e) {
            this.getEmployees(e)
        },
        getArticleInfo(data) {
            const this_ = this
            $.ajax({
                url: DomainName + '/getArticleInfoAction?token=' + $.cookie("token"),
                type: 'POST',
                data,
                success: function (res) {
                    console.log('商品', res.tableData)
                    this_.tableData = res.tableData
                    this_.total1 = Number(res.limit)
                }
            })
        },
        CategorysChange(e) {
            console.log(e)
            if (e == 0) {
                this.getCategorys('category')
            } else {
                this.getCategorys('brand')
            }
        },
        ArticlesChange(e) {
            console.log('ArticlesChange', e)
            if (e.length > 0) {
                if (this.cagie == '1') {
                    this.IO_form.Brand_SerialNumber = JSON.stringify(e)
                    console.log(this.IO_form)
                } else {
                    this.IO_form.Category_SerialNumber = JSON.stringify(e)
                    console.log(this.IO_form)
                }
                this.getArticleInfo(this.IO_form)
            }
        },
        GoodsChange(e) {
            console.log('GoodsChange', e)
            console.log(this.Goods);
            // if (e.length > 0) {
            //     if (this.cagie=='1') {
            //         this.IO_form.Goods_Brand_SerialNumber = JSON.stringify(e)
            //         console.log(this.IO_form)
            //     } else {
            //         this.IO_form.Goods_Brand_SerialNumber = JSON.stringify(e)
            //         console.log(this.IO_form)
            //     }
            //     this.getArticleInfo(this.IO_form)
            // }
        },
        EmployeesChange(e) {
            console.log(e)
            let arr = []
            e.forEach(item => {
                arr.push(item[1])
            })
            this.IO_form1.Department_SerialNumber = JSON.stringify(arr)
            this.getAllEmployees(this.IO_form1)
        },
        getAllEmployees(data) {
            const this_ = this
            $.ajax({
                url: DomainName + '/getEmployeeIDAction?token=' + $.cookie("token"),
                type: 'POST',
                data,
                success: function (res) {
                    console.log(res)
                    this_.tableData1 = res.Data
                    this_.total2 = res.Data.length
                }
            })
        },
        getCategorys(Data) {
            const this_ = this
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Data,
                    isNullEnterprise: true
                },
                success: function (res) {
                    console.log(res)
                    this_.ArticleByCategorys = res
                }
            })
        },
        getStores() {//获取所有店铺
            const this_ = this
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                data: {
                    Data: "store_info",
                },
                success: function (res) {
                    console.log(res);
                    this_.Stores = res
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        getCardpage() {//获取所有版面
            const this_ = this
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                data: {
                    Data: "prepayment_card_page",
                },
                success: function (res) {
                    console.log(res);
                    this_.Cardpages = res
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        SelectChange(e) {
            // this.Cardpages
            this.Cardpages.forEach((item, i) => {
                if (item.Prepayment_Card_Page_SerialNumber == e) {
                    this.CardpagesImg = item.Picture_URL
                }
            })
            this.srcList = []
            this.srcList.push(DomainIMG + this.CardpagesImg)
        },
        AddStore() {//增加店铺
            this.form.Children.gift_card_scheme_storescope.push({//适用店铺表
                Gift_Card_Scheme_StoreScope_SerialNumber: "",//主键
                Gift_Card_Scheme_SerialNumber: "",//外键
                Store_SerialNumber: "",//门店流水号表
            })
        },
        RdcStore() {//减少店铺
            if (this.form.Children.gift_card_scheme_storescope.length > 1) {
                this.form.Children.gift_card_scheme_storescope.pop()
            } else {
                this.$message({
                    message: '不可以再删除了',
                    type: 'error'
                });
            }
        },
        add_gift_card_scheme_article() {//增加
            this.form.Children.gift_card_scheme_article.push({//适用店铺表
                // {//适用商品表
                sGift_Card_Scheme_Article_SerialNumber: "",//主键
                Gift_Card_Scheme_SerialNumber: "",//外键
                Article_SerialNumber: "",//商品主键
                Quantity: "",
                // }
            })
        },
        del_gift_card_scheme_article() {//减少
            if (this.form.Children.gift_card_scheme_article.length > 1) {
                this.form.Children.gift_card_scheme_article.pop()
            } else {
                this.$message({
                    message: '不可以再删除了',
                    type: 'error'
                });
            }
        },
        submit(formName) {//提交表单
            const _this = this;
            console.log(this.form.Children.gift_card_scheme_article[0].Quantity!=0)
            // return;
            this.$refs[formName].validate((valid) => {
                // form: {//储值卡方案表
                //     Giftt_Card_Scheme_SerialNumber: "",//储值卡方案流水号
                //     Gift_Card_Scheme_Name: '',//储值卡方案名称
                //     Gift_Card_Quantity: '',
                //     Effective_Date: '',//有效期(长期有效为1可选)
                //     Gift_Card_Page_SerialNumber: '',//储值卡版面
                //     Start_Date: '',//上架时间
                //     End_Date: '',//结束时间
                //     Gift_Card_Article_Quantity: '',//付款方式(1微信支付,2支付宝支付,3电汇)
                //     Buyer_Enterprise_Info_SerialNumber: '',//被制卡企业
                //     Enterprise_Info_SerialNumber: $.cookie("enterprise"),//本企
                //     Children: {
                //         gift_card_scheme_storescope: [{//适用店铺表
                //             Gift_Card_Scheme_StoreScope_SerialNumber: "",//主键
                //             Gift_Card_Scheme_SerialNumber: "",//外键
                //             Store_SerialNumber: "",//门店流水号表
                //         }],
                //         gift_card_scheme_article: [{//适用商品表
                //             Gift_Card_Scheme_Article_SerialNumber: "",//主键
                //             Gift_Card_Scheme_SerialNumber: "",//外键
                //             Article_SerialNumber: "",//商品主键
                //             Quantity: "",
                //         }],
                //     }
                if (valid
                    &&this.form.Children.gift_card_scheme_article[0].Quantity!=''
                    &&this.form.Children.gift_card_scheme_article[0].Quantity!=0
                    &&this.form.Gift_Card_Quantity!=0
                    &&this.form.Gift_Card_Quantity!=''
                    &&this.form.Gift_Card_Scheme_Name!=''
                    &&this.form.Gift_Card_Article_Quantity!=''
                ) {
                    this.loading = true
                    console.log(_this.form)
                    $.ajax({
                        type: 'POST',
                        url: BASE_URL + '/GiftCard/addProductCardProgramme',
                        // JSON.stringify(_this.form2)
                        data: {
                            Data: JSON.stringify({ gift_card_scheme: [this.form] }),
                            token: $.cookie("token")
                        },
                        complete: function (XMLHttpRequest) {
                            _this.loading = false
                            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                                window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                            }
                        },
                        success: function (Result) {
                            console.log('111', Result)
                            if (Result.status == 0) {
                                _this.$message.success('提交成功')
                                window.history.go(-1)
                            }
                            
                        },
                        error: function () {
                            _this.$message.error("操作失败，请稍后再试")
                        },
                        dataType: "json"
                    });
                } else {
                    this.$message({
                        message: '请检查表单',
                        type: 'warning'
                    });
                }
            });
        },
        getTime(date, num) {
            let data = new Date(date).getTime() + (num * 24 * 60 * 60 * 1000)
            return getYMDHMSDate(data)
        },
        DeleteEmployee(i) {
            if (this.tableData1.length > 1) {
                this.tableData1.splice(i, 1)
                this.total2--
            } else {
                this.$message({
                    message: '不可以再删除了',
                    type: 'error'
                });
            }
        }
    }
})